/*
 * frmInicio2.java
 *
 * Created on 6 de septiembre de 2007, 05:03 PM
 */

package prototipo;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import ajedrezLogica.*;
import BL.*;
import BE.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;

/**
 *
 * @author  Billy
 */
public class frmInicio extends javax.swing.JInternalFrame {
    JDesktopPane jDesktopPane1;
    torneoBL tBL;
    torneo unTorneo;
    int codTorneoSeleccionado = 0;
    DefaultTableModel dtm;
    ArrayList <torneo> arrtBL;
    frmAdmTorneo fat;    
    
    /** Creates new form frmInicio2 */
    public frmInicio(JDesktopPane jDesktopPane1) {
        initComponents();
        tBL = new torneoBL();
        this.jDesktopPane1 = jDesktopPane1;
        this.llenarGrillaTorneos();
        this.btnAbrirTorneos.setEnabled(false);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        btnIngresarPartidas = new javax.swing.JButton();
        btnLeerPartidas = new javax.swing.JButton();
        btnBuscarPartidas = new javax.swing.JButton();
        btnJuego = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblTorneosAbiertos = new javax.swing.JTable();
        btnNuevoTorneo = new javax.swing.JButton();
        btnAbrirTorneos = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Pantalla Principal");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ver/Editar Partidas"));

        btnIngresarPartidas.setText("Ingresar Partidas");
        btnIngresarPartidas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIngresarPartidasActionPerformed(evt);
            }
        });

        btnLeerPartidas.setText("Leer Partidas");
        btnLeerPartidas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLeerPartidasActionPerformed(evt);
            }
        });

        btnBuscarPartidas.setText("Buscar Partidas");
        btnBuscarPartidas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarPartidasActionPerformed(evt);
            }
        });

        btnJuego.setText("Jugar Partidas");
        btnJuego.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnJuegoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnIngresarPartidas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLeerPartidas, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBuscarPartidas, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnJuego, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLeerPartidas)
                    .addComponent(btnBuscarPartidas)
                    .addComponent(btnJuego)
                    .addComponent(btnIngresarPartidas))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Torneos Abiertos"));

        tblTorneosAbiertos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Cod", "nombre de torneo", "fecha Inicio", "fecha Final"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblTorneosAbiertos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblTorneosAbiertosMouseClicked(evt);
            }
        });
        tblTorneosAbiertos.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblTorneosAbiertosKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tblTorneosAbiertos);

        btnNuevoTorneo.setText("Crear Torneo");
        btnNuevoTorneo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoTorneoActionPerformed(evt);
            }
        });

        btnAbrirTorneos.setText("Abrir Torneos");
        btnAbrirTorneos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAbrirTorneosActionPerformed(evt);
            }
        });

        btnEliminar.setText("Eliminar");
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(btnNuevoTorneo)
                        .addGap(24, 24, 24)
                        .addComponent(btnAbrirTorneos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                        .addComponent(btnEliminar)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEliminar)
                    .addComponent(btnAbrirTorneos)
                    .addComponent(btnNuevoTorneo))
                .addContainerGap())
        );

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/prototipo/logoChessAdmin3.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnSalir)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(jLabel3)))
                .addContainerGap(14, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarPartidasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarPartidasActionPerformed
// TODO add your handling code here:
        frmBuscarPartidas fbp = new frmBuscarPartidas(this.jDesktopPane1);
        this.jDesktopPane1.add(fbp);
        fbp.setVisible(true);            
    }//GEN-LAST:event_btnBuscarPartidasActionPerformed

    private void btnJuegoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnJuegoActionPerformed
// TODO add your handling code here:
        frmJuegoPartidas fjp = new frmJuegoPartidas();
        this.jDesktopPane1.add(fjp);
        fjp.setVisible(true); 
    }//GEN-LAST:event_btnJuegoActionPerformed

    private void btnAbrirTorneosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAbrirTorneosActionPerformed
// TODO add your handling code here:
        fat = new frmAdmTorneo(unTorneo, this.jDesktopPane1);
        this.jDesktopPane1.add(fat);
        fat.setVisible(true);            
    }//GEN-LAST:event_btnAbrirTorneosActionPerformed

    private void tblTorneosAbiertosKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblTorneosAbiertosKeyPressed
// TODO add your handling code here:
            if( evt.getKeyCode() == KeyEvent.VK_DELETE ){
            int row = this.tblTorneosAbiertos.getSelectedRow();
            if(row>-1){
                this.dtm.removeRow(row);
            }
        }
    }//GEN-LAST:event_tblTorneosAbiertosKeyPressed

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
// TODO add your handling code here:
            int row = this.tblTorneosAbiertos.getSelectedRow();
            if(row>-1){
                int rpta = JOptionPane.showConfirmDialog(this,"Desea eliminar el registro","Informacion",JOptionPane.INFORMATION_MESSAGE);        
                if(rpta ==JOptionPane.YES_OPTION){
                    String strcod = this.tblTorneosAbiertos.getValueAt(row, 0)+"";
                    int codTorneo = Integer.parseInt(strcod);
                    this.tBL.actualizarEstadoEliminacion(codTorneo);                
                    //this.dtm.removeRow(row);     
                    this.llenarGrillaTorneos();                
                }
            }     
            else JOptionPane.showMessageDialog(this,"No se ha seleccionado registro","Informacion",JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void tblTorneosAbiertosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblTorneosAbiertosMouseClicked
// TODO add your handling code here:
        int fila= this.tblTorneosAbiertos.getSelectedRow();
        unTorneo = this.arrtBL.get(fila);
        //this.tBL.getLstTorneos().get(fila);      
        if(fila>=0) this.btnAbrirTorneos.setEnabled(true);
    }//GEN-LAST:event_tblTorneosAbiertosMouseClicked

    public JTable dameTablaTorneos(){
        return tblTorneosAbiertos;
    }
    
    private void btnLeerPartidasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLeerPartidasActionPerformed
// TODO add your handling code here:
        try{
            frmLecturaPartidas flp = new frmLecturaPartidas(this.jDesktopPane1);
            this.jDesktopPane1.add(flp);
            flp.setVisible(true);        
        }
        catch(Exception ex){
            System.out.println(ex.getMessage());
            ex.printStackTrace();        
        }
    }//GEN-LAST:event_btnLeerPartidasActionPerformed

    private void btnNuevoTorneoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoTorneoActionPerformed
// TODO add your handling code here:
        frmNuevoTorneo fnt2 = new frmNuevoTorneo(this);
        this.jDesktopPane1.add(fnt2);
        fnt2.setVisible(true);
        
    }//GEN-LAST:event_btnNuevoTorneoActionPerformed

    private void btnIngresarPartidasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIngresarPartidasActionPerformed
// TODO add your handling code here:
        frmIngresoPartidas fip = new frmIngresoPartidas(/*fat,*/this.jDesktopPane1);
        this.jDesktopPane1.add(fip);
        fip.setVisible(true);
    }//GEN-LAST:event_btnIngresarPartidasActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnSalirActionPerformed

    public void llenarGrillaTorneos(){
        
        int ano, ano2, mes, mes2, dia, dia2;
        arrtBL = new ArrayList<torneo>();       
        Vector rows = new Vector();
        
        for( torneo tBE : this.tBL.getLstTorneos()){
            arrtBL.add(tBE);
            Vector row = new Vector();
            row.add(tBE.getIdTorneo());            
            row.add(tBE.getnombreTorneo());

            ano = tBE.getFechaInicio().get(Calendar.YEAR);            
            mes = tBE.getFechaInicio().get(Calendar.MONTH)+1;
            dia = tBE.getFechaInicio().get(Calendar.DATE);                        
//            System.out.println("1::"+ ano+"/"+mes+"/"+dia);
//            System.out.println("1::"+ tBE.getFechaInicio().getTime().toString());
            
            row.add(ano+"/"+mes+"/"+dia);            
            
            //row.add(tBE.getFechaInicio().getTimeInMillis());
            //row.add(tBE.getFechaFin().getTime().toString());
            
            ano2 = tBE.getFechaFin().get(Calendar.YEAR);            
            mes2 = tBE.getFechaFin().get(Calendar.MONTH)+1;
            dia2 = tBE.getFechaFin().get(Calendar.DATE);                        
//            System.out.println("2::"+ ano2+"/"+mes2+"/"+dia2);            
            
            row.add(ano2+"/"+mes2+"/"+dia2);            
            rows.add(row);
        }
        
        Vector colNames = new Vector();        
        for (int i=0;i<this.tblTorneosAbiertos.getColumnCount();i++){
            colNames.add(this.tblTorneosAbiertos.getColumnName(i));
        }
             
        //dtm = new DefaultTableModel(rows,colNames);
        dtm = new DefaultTableModel(rows,colNames) {
                public boolean isCellEditable(int row, int column) {
                    return false;
                }                  
           };          
        this.tblTorneosAbiertos.setModel(dtm);
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAbrirTorneos;
    private javax.swing.JButton btnBuscarPartidas;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnIngresarPartidas;
    private javax.swing.JButton btnJuego;
    private javax.swing.JButton btnLeerPartidas;
    private javax.swing.JButton btnNuevoTorneo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblTorneosAbiertos;
    // End of variables declaration//GEN-END:variables
    
}
